package com.upload.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

//For XML Read Write
import javax.xml.parsers.*;

import org.w3c.dom.*;


import javax.xml.xpath.*;

@Controller

public class CheckboxController {
	//private List<User> userList = new ArrayList<User>(); 
	@RequestMapping(value="/AddUser.htm",method = RequestMethod.GET)
	public @ResponseBody String showRegistration(Map model) {
		//JSONObject json = new JSONObject();
		//json.put("city", "Mumbai");
		//json.put("country", "India");	 

		//String output = json.toString();
		return "GET ";
	}
	

	// Process the form.
	@RequestMapping(value="/getcheckbox.do",method=RequestMethod.POST)
	public @ResponseBody String addUser(Map model){
		JSONObject json = new JSONObject();
		List<String> list = new ArrayList<String>();
		
		try {

			File fXmlFile = new File("d:\\checkbox.xml");
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(fXmlFile);
			doc.getDocumentElement().normalize();

			XPathFactory xPathfactory = XPathFactory.newInstance();
			XPath xpath = xPathfactory.newXPath();
			XPathExpression expr = xpath.compile("//checkbox");
			NodeList nList = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

			for (int temp = 0; temp < nList.getLength(); temp++) { 

				Node nNode = nList.item(temp);
				if (nNode.getNodeType() == Node.ELEMENT_NODE) {

					Element eElement = (Element) nNode;
					list.add('"'+getTagValue("id", eElement)+'"');
					list.add('"'+getTagValue("value", eElement)+'"');
					//list.add('"'+getTagValue("url", eElement)+'"');	
					
					json.accumulate("chk"+temp, list);
					list=new ArrayList<String>();
					//System.out.println("ID : " + getTagValue("id", eElement));
					//System.out.println("value : " + getTagValue("value", eElement));
					//System.out.println("url : " + getTagValue("url", eElement));
					
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}


		String output = json.toString();
		return output;
	}
	 private static String getTagValue(String sTag, Element eElement) {
			NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
		 
		        Node nValue = (Node) nlList.item(0);
		 
			return nValue.getNodeValue();
		  }
	 @RequestMapping(value="/getcheckboxurl.do",method=RequestMethod.POST)
		public @ResponseBody String getUrl(Map model,@RequestParam String id){
		 
			
			String box_id=null;
			try {

				File fXmlFile = new File("d:\\checkbox.xml");
				DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
				DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
				Document doc = dBuilder.parse(fXmlFile);
				doc.getDocumentElement().normalize();

				XPathFactory xPathfactory = XPathFactory.newInstance();
				XPath xpath = xPathfactory.newXPath();
				XPathExpression expr = xpath.compile("//checkbox[@id='"+id+"']");
				NodeList nList = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

				for (int temp = 0; temp < nList.getLength(); temp++) { 

					Node nNode = nList.item(temp);
					if (nNode.getNodeType() == Node.ELEMENT_NODE) {

						Element eElement = (Element) nNode;
						return getTagValue("url", eElement);
						
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

			return "true"; 
			
		}
}
